package com.probiz.estore.customer.service.impl;

import java.util.Calendar;
import java.util.Date;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.customer.ReviewEligibility;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.ReviewEligibilityDao;
import com.probiz.estore.customer.service.ReviewEligibilityManager;


/**
 * Manager implementation for ReviewEligibility, responsible for business processing, and communicate between web and persistence layer.
 */
public class ReviewEligibilityManagerImpl extends GenericManagerImpl<ReviewEligibility> implements ReviewEligibilityManager {

	private ReviewEligibilityDao reviewEligibilityDao = null;

	/**
	 * @param reviewEligibilityDao
	 *            the reviewEligibilityDao to set
	 */
	public void setReviewEligibilityDao(ReviewEligibilityDao reviewEligibilityDao) {
		this.reviewEligibilityDao = reviewEligibilityDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = reviewEligibilityDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ReviewEligibility entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ReviewEligibility entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ReviewEligibilityManager#addEligibility(java.lang.Integer, java.lang.Integer)
	 */
	public void addEligibility(Integer customerId,Integer productId){
		ReviewEligibility eligibility = reviewEligibilityDao.getReviewEligibilityByPCustomerIdAndProductId(customerId,productId);
		if(eligibility == null){
			eligibility = new ReviewEligibility();
			eligibility.setCustomerId(customerId);
			eligibility.setProductId(productId);
			eligibility.setRemainedCount(Integer.valueOf(1));
		}else{
			eligibility.setRemainedCount(eligibility.getRemainedCount().intValue()+1);
		}
		Calendar date = Calendar.getInstance();
		date.set(Calendar.DAY_OF_YEAR, date.get(Calendar.DAY_OF_YEAR)+ConfigUtil.getInstance().getReviewExpiredDays().intValue());
		eligibility.setExpiredTime(date.getTime());
		reviewEligibilityDao.save(eligibility);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ReviewEligibilityManager#getEligibility(java.lang.Integer, java.lang.Integer)
	 */
	public Short getEligibility(Integer customerId,Integer productId){
		ReviewEligibility eligibility = reviewEligibilityDao.getReviewEligibilityByPCustomerIdAndProductId(customerId,productId);
		if(null == eligibility){
			return ReviewEligibility.RESULT_UNAVAILABLE;
		}else if(eligibility.getRemainedCount().intValue()<=0){
			return ReviewEligibility.RESULT_OVERCOUNT;
		}else if(eligibility.getExpiredTime().compareTo(new Date()) < 0){
			return ReviewEligibility.RESULT_OVERTIME;
		}else{
			return ReviewEligibility.RESULT_AVAILABLE;
		}
	}

}
